/*
 * Copyright 2014 Alex Curran
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.espian.showcaseview.sample;

import com.github.amlcurran.showcaseview.LocalHiLog;
import com.github.amlcurran.showcaseview.ShowcaseDrawer;
import com.github.amlcurran.showcaseview.ShowcaseView;
import com.github.amlcurran.showcaseview.targets.ViewTarget;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.render.BlendMode;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.Texture;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.global.resource.ResourceManager;
import ohos.media.image.PixelMap;

/**
 * 自定义展示UI
 */
public class CustomShowcaseAbility extends Ability {
    ShowcaseView mShowcaseView;


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        ComponentContainer componentContainer = (ComponentContainer) LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_ability_custom_showcase, null, false);
        setUIContent(componentContainer);

        mShowcaseView = new ShowcaseView.Builder(this, (ComponentContainer) componentContainer.getComponentParent())
                .setTarget(new ViewTarget(ResourceTable.Id_imageView, this))
                .setContentTitle(ResourceTable.String_custom_text_painting_title)
                .setContentText(ResourceTable.String_custom_text_painting_text)
                .setShowcaseDrawer(new CustomShowcaseView(getResourceManager()))
                .build();
    }


    private static class CustomShowcaseView implements ShowcaseDrawer {
        private float width = 0;
        private float height = 0;
        private Paint eraserPaint = null;
        private Paint basicPaint = null;
        private int eraseColour = 0xffffff;
        private RectFloat renderRect = null;

        CustomShowcaseView(ResourceManager resources) {
            try {
                width = resources.getElement(ResourceTable.Float_custom_showcase_width).getFloat();
                height = resources.getElement(ResourceTable.Float_custom_showcase_height).getFloat();
                eraserPaint = new Paint();
                eraserPaint.setColor(new Color(0xFFFFFF));
                eraserPaint.setAlpha(0);
                eraserPaint.setBlendMode(BlendMode.CLEAR);
                eraserPaint.setAntiAlias(true);
                eraseColour = resources.getElement(ResourceTable.Color_custom_showcase_bg).getColor();
                basicPaint = new Paint();
                renderRect = new RectFloat();
            } catch (Exception e) {
                LocalHiLog.warn("ShowcaseView", "width or height not found");
            }
        }

        @Override
        public void setShowcaseColour(int color) {
            eraserPaint.setColor(new Color(color));
        }

        @Override
        public void drawShowcase(PixelMap buffer, float x, float y, float scaleMultiplier) {
            Texture texture = new Texture(buffer);
            Canvas bufferCanvas = new Canvas(texture);
            renderRect.left = x - width / 2f;
            renderRect.right = x + width / 2f;
            renderRect.top = y - height / 2f;
            renderRect.bottom = y + height / 2f;
            bufferCanvas.drawRect(renderRect, eraserPaint);
        }

        @Override
        public void drawShowcaseBorder(Canvas canvas, float x, float y) {
        }

        @Override
        public int getShowcaseWidth() {
            return (int) width;
        }

        @Override
        public int getShowcaseHeight() {
            return (int) height;
        }

        @Override
        public float getBlockedRadius() {
            return width;
        }

        @Override
        public void setBackgroundColour(int backgroundColor) {
            // No-op, remove this from the API?
        }

        @Override
        public void erase(PixelMap bitmapBuffer) {
            bitmapBuffer.writePixels(eraseColour);
        }

        @Override
        public void drawToCanvas(Canvas canvas, PixelMap bitmapBuffer) {
            PixelMapHolder pixelMapHolder = new PixelMapHolder(bitmapBuffer);
            canvas.drawPixelMapHolder(pixelMapHolder, 0, 0, basicPaint);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        mShowcaseView.destory();
    }
}
